package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.MenuService;
import com.iot08.admin.service.MobNotifyPushService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.MobNotifyPush;
import com.iot08.common.response.Response;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description: 推送管理
 * @author: Rock
 * @time: 2020/9/11 17:34
 */
@Controller
@RequestMapping("notifyPush")
public class MobNotifyPushController extends BaseController {

    @Resource
    private MobNotifyPushService mobNotifyPushService;
    @Resource
    private MenuService menuService;

    /**
     * @param page
     * @param target
     * @time: 2020/12/26 10:53
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page, Integer target) {
        ModelAndView mv = new ModelAndView();
        Page<MobNotifyPush> pages = mobNotifyPushService.list(page, target, this.getSessionUser());
        mv.addObject("notifyPushs", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("target", target);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("menu", menuService.getByName("创建推送"));
        mv.setViewName("notify/notify_push_list");
        return mv;
    }

    /**
     * @time: 2020/12/28 15:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toNotifyPush() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("notify/notify_push");
        return mv;
    }

    /**
     * @time: 2020/12/28 15:37
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("add")
    public Response<Object> addNotifyPush(MobNotifyPush notifyPush, MultipartFile file) throws Exception {
        return mobNotifyPushService.add(notifyPush, file, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * 撤回
     *
     * @param id
     * @time: 2020/12/30 15:54
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("recall")
    public Response<Object> recall(String id) {
        return mobNotifyPushService.recall(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @param id
     * @time: 2020/12/30 16:02
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @GetMapping("detail")
    public Response<Object> detail(String id) {
        return mobNotifyPushService.detail(id);
    }

    @ResponseBody
    @PostMapping("cancel")
    public Response<Object> cancel(String id) {
        return mobNotifyPushService.cancel(id, this.getSessionUser(), this.getRemortIP());
    }

}
